# -*- coding: utf-8 -*-
"""
Created on Fri Aug 10 11:08:57 2018

@author: jiujiujiu
"""

import requests
import re
import time
from cs import day
from WriteData import writedata
from Tk import genearteMD5


# 获取单个页面
def getHTMLText(url):
    header = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8'}
    try:

        r = requests.get(url, timeout=30, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常
        r.encoding = r.apparent_encoding
        return r.text

    except:
        return None


def get_data_Zx(url):
    html = getHTMLText(url)
    if not html:
        return None
    #    rr = re.compile(r'<li class="left newleft m-news-timer"><i></i>([\s\S]*?)</li>')
    rr = re.compile(r'<span class="timego" title="([\s\S]*?) \d{2}:\d{2}:\d{2}">[\s\S]*?</span>')
    datas = re.findall(rr, html)
    return datas


def count_process_one():
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 0
    while 1:
        url = url = 'http://www.bt110.com/news/index.html?page={}'
        url = url.format(a)
        dates = get_data_Zx(url)
        if not dates:
            break
        for date in dates:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        print('----BT110资讯第{}页获取中----'.format(a))
        if d4 > 0:
            break
    return (d1, d2, d3)


def postHTMLText(url, data):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:
        r = requests.post(url, timeout=30, data=data, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常
        r.encoding = r.apparent_encoding
        html = r.text
        return html

    except:
        return None


def get_data_kx(url, data):
    l = []
    id = 0
    html = postHTMLText(url, data)
    if not html:
        return None
    html = eval(html)
    datas = html['list']['data']
    for dt in datas:
        date = time.strftime("%Y-%m-%d %X", time.localtime(dt['cat_time']))[:10]
        id = dt['id']
        l.append(date)
    return (l, id)


def count_process_two():  # 快讯
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    id = 99999999
    while 1:
        data = {'isInit': '2',
                'id': id}
        url = 'http://www.bt110.com/kuaixun/index/getlist'
        dates = get_data_kx(url, data)
        if not dates:
            break
        id = dates[1]
        for date in dates[0]:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        print('----BT110快讯第{}页获取中----'.format(a))
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = 'BT110'
    t1 = count_process_one()
    t2 = count_process_two()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0] + t2[0], "yesterday": t1[1] + t2[1], "frontday": t1[2] + t2[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
